package cn.hiapi.shopkeeper.service.activity;

import cn.hiapi.basic.exception.BasicException;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.pay.IPaySuccessCallback;
import cn.hiapi.basic.pay.model.SuccessModel;
import cn.hiapi.basic.service.AbsBasicService;
import cn.hiapi.shopkeeper.entity.activity.ActivityLotteryAd;
import cn.hiapi.shopkeeper.enums.ActivityType;
import cn.hiapi.shopkeeper.jpa.activity.ActivityLotteryAdJpa;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author AdinZ
 * @date 2023/2/4 15:07
 */
@Service
public class ActivityLotteryAdService extends AbsBasicService<ActivityLotteryAdJpa, ActivityLotteryAd, Long> implements IPaySuccessCallback {

    @Override
    public String getType() {
        return ActivityType.LOTTERY_AD.name();
    }

    @Override
    @Transactional
    public boolean payCallback(SuccessModel model) throws BasicException {
        ActivityLotteryAd ad = this.get(model.getFid());
        if (ad==null)throw new BasicException("活动不存在");
        if (ad.getPayStatus()!=0)throw new BasicException("状态错误");
        ad.setPayStatus(1);
        ad.setUpdated(System.currentTimeMillis()    );
        this.save(ad);
        return true ;
    }

    @Transactional
    public void start(List<Long> collect) {
        if (collect.isEmpty())return;
        List<ActivityLotteryAd> list = this.findList(new QueryWrapper().in("id", collect).eq("payStatus", 1).eq("status", 0));
        list.forEach(e->{
            e.setStatus(1);
            e.setUpdated(System.currentTimeMillis() );
        });

        this.save(list);
    }

    @Transactional
    public void end(List<Long> collect) {
        if (collect.isEmpty())return;
        List<ActivityLotteryAd> list = this.findList(new QueryWrapper().in("id", collect).eq("payStatus", 1).eq("status", 1));
        list.forEach(e->{
            e.setStatus(2);
            e.setUpdated(System.currentTimeMillis() );
        });

        this.save(list);
    }



}
